<!-- 人脉设置 -->
<template>
  <a-form-model layout="horizontal" :model="form" :labelCol="labelCol" :wrapperCol="wrapperCol">
    <a-form-model-item label="人脉地图展示" extra="控制小程序的人脉列表展示内容">
      <div>
        <span>
          <span class="_mr-10">人脉列表</span>
          <a-switch v-model="form.listShowUser" :disabled="!form.listShowUnit" />
        </span>
        <span class="_ml-20">
          <span class="_mr-10">单位列表</span>
          <a-switch v-model="form.listShowUnit" :disabled="!form.listShowUser" />
        </span>
      </div>
    </a-form-model-item>
    <a-form-model-item label="名片详情查看权限" extra="控制查看名片详情的权限">
      <div>
        <span>
          <span class="_mr-10">人脉名片</span>
          <a-switch v-model="form.detailShowUser" checked-children="所有人可见" un-checked-children="仅会员可见" />
        </span>
        <span class="_ml-20">
          <span class="_mr-10">单位名片</span>
          <a-switch v-model="form.detailShowUnit" checked-children="所有人可见" un-checked-children="仅会员可见" />
        </span>
      </div>
    </a-form-model-item>
    <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
      <a-button type="primary" @click="handleSave">保存</a-button>
    </a-form-model-item>
  </a-form-model>
</template>

<script>
import { InnerTenantInfo, InnerTenantSave } from '@/api/system/base'
export default {
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        listShowUser: false,
        listShowUnit: false,
        detailShowUser: false,
        detailShowUnit: false
      },
      dataSource: []
    }
  },
  methods: {
    async getInnerTenantInfo() {
      let res = await InnerTenantInfo()
      let list = res.data || []
      this.dataSource = list
      this.form.listShowUser = list[0] && list[0].listShow ? false : true
      this.form.listShowUnit = list[1] && list[1].listShow ? false : true

      this.form.detailShowUser = list[0] && list[0].detailShow ? false : true
      this.form.detailShowUnit = list[1] && list[1].detailShow ? false : true
    },
    async handleSave() {
      function convertVal(val) {
        return val ? 0 : 1
      }

      let list = this.dataSource.map((item, index) => {
        return {
          ...item,
          listShow: index === 0 ? convertVal(this.form.listShowUser) : convertVal(this.form.listShowUnit),
          detailShow: index === 0 ? convertVal(this.form.detailShowUser) : convertVal(this.form.detailShowUnit)
        }
      })
      try {
        await InnerTenantSave([...list])
        this.$message.success('保存成功')
      } catch (error) {
        this.getInnerTenantInfo()
      }
    }
  },
  created() {
    this.getInnerTenantInfo()
  },
  mounted() {}
}
</script>
<style scoped lang="less"></style>
